package org.openurp.edu.student.info.web.action

import java.io.OutputStream
import org.beangle.commons.lang.ClassLoaders
import org.beangle.data.transfer.importer.listener.ImporterForeignerListener
import org.beangle.webmvc.api.annotation.ignore
import org.beangle.webmvc.api.view.Stream
import org.beangle.webmvc.api.view.View
import org.beangle.webmvc.entity.action.RestfulAction
import org.openurp.edu.student.info.model.Paystate
import org.openurp.edu.student.info.model.Paytype
import org.openurp.edu.student.info.model.Scholarship
import org.openurp.edu.student.info.model.Schooling
import org.openurp.edu.student.info.model.Stdtype
import org.openurp.edu.student.info.web.helper.ExportExcelUtils
import org.openurp.edu.student.info.web.helper.ImportSchoolingListener
import org.beangle.webmvc.api.context.Params
import org.beangle.data.transfer.TransferListener
import org.beangle.webmvc.api.action.ServletSupport
import org.beangle.webmvc.api.annotation.response

/**
 * @author King Sun
 */
class PaymentfinAction extends RestfulAction[Schooling] with ImportDataSupport[Schooling]  with ServletSupport {

override protected def editSetting(entity: Schooling): Unit = {
    put("stdtypes", entityDao.getAll(classOf[Stdtype]))
    put("paystates", entityDao.getAll(classOf[Paystate]))
    put("paytypes", entityDao.getAll(classOf[Paytype]))
    put("scholarships", entityDao.getAll(classOf[Scholarship]))
  }

  override  def search(): String = {
    put("math",new Math2)
    super.search()
  }
    
  override protected def saveAndRedirect(schooling: Schooling): View = {
    if (schooling.paystate == null) {
      schooling.paystate = new Paystate
      schooling.paystate.id = 1L
    }
    super.saveAndRedirect(schooling)
  }

  def importTemplate: View = {
    Stream(ClassLoaders.getResourceAsStream("schooling.xls"), "application/vnd.ms-excel", "交费信息管理.xls")
  }

  protected override def importerListeners: List[_ <: TransferListener] = {
    List(new ImporterForeignerListener(entityDao), new ImportSchoolingListener(entityDao))
  }
  
  def exportpayment(): View = {
    val list = collection.JavaConversions.asJavaCollection(entityDao.search(this.getQueryBuilder()))
    //查出学生信息并放到map中
    val beans = new java.util.HashMap[String, Any]
    beans.put("list", list);
    //获得模板路径
    val path = ClassLoaders.getResourceAsStream("student_payment.xls");
    //准备输出流
    var os: OutputStream = null;
    response.setContentType("application/x-excel");
    response.setHeader("Content-Disposition", "attachment;filename=studentPay.xls");
    os = response.getOutputStream();
    ExportExcelUtils.excprtStudentExcel(beans, path, os);
    //val data=Params.sub("student")
    null
  }
}

class Math2{
  def parse(s:String):Double={
        if(null==s ) 0d
        else java.lang.Double.parseDouble(s)
      }
}

